import { Node, Button, Label, EditBox, PageView, ProgressBar, Slider, Component } from 'cc';
import { UICompType } from '../base/JeesEnum';

// 页面UI容器 用于存放和搜索一个页面中的节点和各种组件
export class JeesContainer {

    // 不同类型节点Map
    private _mapNode: Map<string, Node | undefined> = new Map();
    private _mapButton: Map<string, Button | undefined> = new Map();
    private _mapLabel: Map<string, Label | undefined> = new Map();
    private _mapSlider: Map<string, Slider | undefined> = new Map();
    private _mapEditBox: Map<string, EditBox | undefined> = new Map();
    private _mapPageView: Map<string, PageView | undefined> = new Map();
    private _mapProgressBar: Map<string, ProgressBar | undefined> = new Map();
    // 组件类型数组
    private _arrType: any[] = [Button, Label, Slider, EditBox, PageView, ProgressBar];
    // 可加载的组件类型数组
    private _arrTypeEnabled: any[] = [];
    // 组件类型Map
    private _arrTypeMap: any[] = [
        this._mapButton,
        this._mapLabel,
        this._mapSlider,
        this._mapEditBox,
        this._mapPageView,
        this._mapProgressBar
    ];

    /**
     * 载入UI结构
     * @param _root UI根节点
     */
    public load(_root: Node, _type_enabled?: any[]) {
        // 设置可加载的组件类型数组
        if (!_type_enabled) _type_enabled = ['ALL'];
        if (_type_enabled[0] === 'ALL') {
            this._arrTypeEnabled = this._arrType;
        } else {
            this._arrTypeEnabled = [];
            for (let i = 0; i < this._arrType.length; i++) {
                let type = this._arrType[i];
                if (_type_enabled.includes(type)) {
                    this._arrTypeEnabled.push(type);
                } else {
                    this._arrTypeEnabled.push(null);
                }
            }
        }
        // 遍历所有子节点
        let children = _root.children;
        for (let i = 0; i < children.length; i++) {
            let childNode = children[i];
            this._mapNode.set(childNode.name, childNode);
            // 遍历判断节点属于什么类型的组件
            for (let i = 0; i < this._arrTypeEnabled.length; i++) {
                let type = this._arrTypeEnabled[i];
                if (type) {
                    let comp = childNode.getComponent(type);
                    // 若类型匹配，存入对应的Map
                    if (comp) {
                        this._arrTypeMap[i].set(childNode.name, comp);
                    }
                }
            }
            this.load(childNode, _type_enabled);
        }
    }

    /**
     * 查找节点
     * @param key 节点名
     */
    getNode(key: string): Node | undefined {
        return this._mapNode.get(key);
    }

    /**
     * 查找组件
     * @param type 组件类型
     * @param key 节点名
     */
    getComp(type: UICompType, key: string): Component | undefined {
        let mapDt = this._arrTypeMap[type];
        if (!mapDt) {
            return undefined;
        }
        return mapDt.get(key);
    }

    /**
     * 查找Button节点
     * @param key 节点名
     */
    getButton(key: string): Button | undefined {
        return this._mapButton.get(key);
    }

    /**
     * 查找Label节点
     * @param key 节点名
     */
    getLabel(key: string): Label | undefined {
        return this._mapLabel.get(key);
    }

    /**
     * 查找Slider节点
     * @param key 节点名
     */
    getSlider(key: string): Slider | undefined {
        return this._mapSlider.get(key);
    }

    /**
    * 查找EditBox节点
    * @param key 节点名
    */
    getEditBox(key: string): EditBox | undefined {
        return this._mapEditBox.get(key);
    }

    /**
    * 查找EditBox节点
    * @param key 节点名
    */
    getProgressBar(key: string): ProgressBar | undefined {
        return this._mapProgressBar.get(key);
    }

    // 可拓展...

}